import { _decorator, Button, Component, HorizontalTextAlignment, instantiate, JsonAsset, Label, Layers, Node, Prefab, Sprite, sys, Tween, tween, UITransform, Vec3 } from 'cc';
import { EDITOR } from 'cc/env';
const { ccclass, property } = _decorator;



@ccclass('BlockDesign')
export class BlockDesign extends Component {
    @property( {type: Number, displayName: "边长" } )
    size: number = 7;

    @property( {type: Number, displayName: "边距" } )
    distance: number = 7;

    @property( {type: Node, displayName: "地板" } )
    baseNode: Node = null;

    @property( {type: Prefab, displayName: "模拟积木块" } )
    designBlock: Node = null;

    @property( {type: Prefab, displayName: "定位预制体" } )
    brickPre: Node = null;

    @property( {type: Sprite, displayName: "当前颜色" } )
    curColor: Sprite = null;

    private selectPlace: Node = null
    private map: number[][] = []
    private currentColor = 1

    protected start(): void {
        // if (cc.engine && cc.engine.isEditor) {
        //     console.log('当前运行在编辑器中。');
        // } else {
        //     console.log('当前运行在游戏引擎中。');
        // }
        this.drawPanel()
    }

    // 绘制地板
    private drawPanel () {
        this.map = Array.from({ length: this.size }, () => Array(this.size).fill(0));

        for (var ix = 0; ix < this.size; ix++) {
            for (var iz = 0; iz < this.size; iz++) {
                const brickNode = instantiate( this.brickPre )
                brickNode.position = new Vec3( ix * this.distance - ( this.distance * (this.size - 1) * 0.5 ), iz * this.distance - ( this.distance * (this.size - 1) * 0.5 ) )

                brickNode.name = ix + "," + iz
                brickNode.parent = this.baseNode
                
                const buttonComponent = brickNode.getChildByName("Button").addComponent(Button);
                buttonComponent.node.on(Node.EventType.TOUCH_END, () => this.itemClicked(buttonComponent.node));
            }
        }
    }

    private itemClicked(sender: Node) {
        // currentColor

        if (sender.getChildByName("new") == null) {
            var blockNode = instantiate( this.designBlock )
            blockNode.name = "new"
            const transform = blockNode.getComponent( UITransform )
            // blockNode.position = new Vec3(-0.5 * this.distance, 0)
            transform.setContentSize( this.distance, this.distance )
            blockNode.getComponent(Sprite).color = this.curColor.color
            blockNode.parent = sender
        } else {
            sender.getChildByName("new").getComponent(Sprite).color = this.curColor.color
        }


        const arr = sender.parent.name.split(",")
        this.map[arr[0]][arr[1]] = this.currentColor
        
        this.selectPlace = sender
    }

    private hengClicked() {
        if (this.selectPlace) {
            var blockNode = instantiate( this.designBlock )
            blockNode.name = "new"
            const transform = blockNode.getComponent( UITransform )
            // blockNode.position = new Vec3(-0.5 * this.distance, 0)
            transform.setContentSize( this.distance, this.distance )
            blockNode.parent = this.selectPlace

            const arr = this.selectPlace.name.split(",")
            this.map[arr[0]][arr[1]] = 100
        }
    }

    private shuClicked() {
        if (this.selectPlace) {
            var blockNode = instantiate( this.designBlock )
            blockNode.name = "new"
            const transform = blockNode.getComponent( UITransform )
            // blockNode.position = new Vec3(-0, this.distance * 0.5)
            transform.setContentSize( this.distance, this.distance )
            blockNode.parent = this.selectPlace

            const arr = this.selectPlace.name.split(",")
            this.map[arr[0]][arr[1]] = 200
        }
    }

    private shanClicked() {
        if (this.selectPlace) {
            if (this.selectPlace.getChildByName("new")) {
                const newNode = this.selectPlace.getChildByName("new")
                newNode.removeFromParent()
                newNode.destroy()

                console.log (this.selectPlace.name)

                const arr = this.selectPlace.parent.name.split(",")
                this.map[arr[0]][arr[1]] = 0
            }
        }
    }

    private outClicked() {
        console.log ( this.map )

        var count = 0
        for (var ix = 0; ix < this.map.length; ix++) {
            for (var iz = 0; iz < this.map[ix].length; iz++) {
                if (this.map[ix][iz] != 0)
                    count++
            }
        }

        console.log ("总块数 = " + count)
    }

    private resetClicked() {
        this.baseNode.removeAllChildren()
        this.drawPanel()
    }

    private colorClicked(event, customEventData) {
        const targetSprite = event.target.getComponent(Sprite)
        this.curColor.color = targetSprite.color
        this.currentColor = parseInt( customEventData )

        console.log ("当前颜色 = " + this.currentColor)
    }
}


